<?php

use App\Models\Business;
use App\Models\User;
use Illuminate\Database\Seeder;

class UserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = \Faker\Factory::create();
        //
        $roles = ['网点主任', '网点副主任', '网点内勤主任', '网点内勤副主任'];
        // 构造查询语句
        $departmentIds = [7, 8, 9, 10, 11, 12, 13, 14];
        $query = null;
        $middles = [];  //中层干部
        foreach ($departmentIds as $id) {
            foreach ($roles as $role) {
                $emps = User::where('department_id', '=', $id)->get();
                foreach ($emps as $emp) {
                    if ($emp->inRoles($roles)) {
                        if (!array_key_exists($id, $middles)) {
                            $middles[$id] = array();
                        }
                        array_push($middles[$id], $emp);
                    }
                }
            }
        }

        $businesses = Business::all();
        foreach ($businesses as $business) {
            $role = $business->role()->first();
            //关联网点人员
            foreach ($departmentIds as $id) {
                $user = $faker->randomElement($middles[$id]);
                $user->roles()->attach($role->id);
            }
        }

    }
}
